#!/usr/bin/env python
from Engine.Const import FPS

class Animation:
    def __init__(self, start_index, last_index, speed, end_event):
        self.image_index = start_index
        self.image_speed = speed
        self.image_direction = 0
        self.image_last = last_index
        self.end_event = end_event #0: go to 0, 1: return, 2: stop
        self.image_next_counter = 0
        
    def update(self):
        image_index_old = self.image_index
        self.image_next_counter += self.image_speed
        if self.image_next_counter >= FPS:
            if self.image_direction == 0:
                if self.image_index == self.image_last:
                    if self.end_event == 0:
                        self.image_index = 0
                    elif self.end_event == 1:
                        self.image_direction = 1
                    else:
                        self.image_speed = 0
                else:
                    self.image_index += 1
            else:
                if self.image_index == 0:
                    self.image_direction = 0
                else:
                    self.image_index -= 1
            self.image_next_counter = 0
            
            
            if image_index_old != self.image_index:
                return self.image_index
        return -1